package cn.star.jots.carousel.service.impl;

import cn.star.jots.carousel.mapper.CarouselMapper;
import cn.star.jots.carousel.pojo.dto.CarouselQuery;
import cn.star.jots.carousel.pojo.dto.CarouselSaveParam;
import cn.star.jots.carousel.pojo.entity.Carousel;
import cn.star.jots.carousel.pojo.vo.CarouselVO;
import cn.star.jots.carousel.service.CarouselService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    CarouselMapper carouselMapper;

    @Override
    public List<CarouselVO> selectAll(CarouselQuery carouselQuery) {
        log.debug("查询轮播图,参数:{}", carouselQuery);
        return carouselMapper.selectAll(carouselQuery);
    }

    @Override
    public void save(CarouselSaveParam carouselSaveParam) {
        log.debug("保存轮播图,参数:{}", carouselSaveParam);
        Carousel carousel = new Carousel();
        BeanUtils.copyProperties(carouselSaveParam, carousel);
        if (carouselSaveParam.getId() == null) {
            carousel.setCreateTime(new Date());
            carouselMapper.insert(carousel);
        } else {
            boolean equals = carouselSaveParam.getStatus().equals("0");
            if (equals) {
                log.debug("下线轮播图,参数:{}",equals);
                carousel.setSort(null);
            }
            carousel.setUpdateTime(new Date());
            carouselMapper.update(carousel);
        }
    }

    @Override
    public void delete(Long id) {
        log.debug("删除轮播图,参数:{}", id);
        carouselMapper.delete(id);
    }

}